package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.homedo.common.bean.data.ResponseData;
import com.homedo.common.exception.HomedoException;
import com.homedo.microservice.open.service.gateway.manager.web.api.IPullServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.PullService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PullServiceRemoteImpl implements IPullServiceRemote {
    private String tarsUserDev = "tarsDev";
    private String tarsUserPwdDev = "99b9654e642c407aaf7e05645d9785fa";

    private String tarsUserFat = "tarsFat";
    private String tarsUserPwdFat = "02438e8d6eae44569f237d545d09657d";

    private String tarsUserUat = "tarsUat";
    private String tarsUserPwdUat = "348dbb887f1c4d58a50e25b2d9208975";

    private String tarsUserPro = "tarsPro";
    private String tarsUserPwdPro = "6172c9e7538347ca82ecb0276d398ad6";


    @Autowired
    private PullService pullService;

    @Override
    public ResponseData in(String gateway, String env, String route, String version, String url, int urlCount, String opUser, String opPwd) {
        this.checkUser(env, opUser, opPwd);
        ResponseData responseData = new ResponseData();
        pullService.dealPullIn(route, version, url, urlCount, null);
        return responseData;
    }


    @Override
    public ResponseData out(String gateway, String env, String route, String url, String opUser, String opPwd) {
        this.checkUser(env, opUser, opPwd);
        ResponseData responseData = new ResponseData();
        pullService.dealPullOut(route, url, null);
        return responseData;
    }

    private void checkUser(String env, String opUser, String opPwd) {
        if (env.equals("dev")) {
            if (!opUser.equals(tarsUserDev) || !opPwd.equals(tarsUserPwdDev)) {
                throw new HomedoException("[用户名/密码错误]");
            }
        } else if (env.equals("fat")) {
            if (!opUser.equals(tarsUserFat) || !opPwd.equals(tarsUserPwdFat)) {
                throw new HomedoException("[用户名/密码错误]");
            }
        } else if (env.equals("uat")) {
            if (!opUser.equals(tarsUserUat) || !opPwd.equals(tarsUserPwdUat)) {
                throw new HomedoException("[用户名/密码错误]");
            }
        } else if (env.equals("pro")) {
            if (!opUser.equals(tarsUserPro) || !opPwd.equals(tarsUserPwdPro)) {
                throw new HomedoException("[用户名/密码错误]");
            }
        }
    }

}
